#include <iostream>
#include <thread>
#include <unistd.h>
#include <vector>
#include <sstream>
#include "fiber.h"
#include "scheduler.h"
#include "log.h"

void show()
{
    // printf("%ld\n", GetThreadId(std::this_thread::get_id()));
    std::ostringstream oss;
    oss << Scheduler::GetThreadId(std::this_thread::get_id()) << " - " << std::this_thread::get_id() << std::endl;
    std::cout << oss.str();
}

void testThreadId()
{
    std::vector<std::thread> v;
    for(int i = 0; i < 5; ++i)
    {
        v.emplace_back(show);
    }

    for(auto& t: v)
    {
        t.join();
    }

}

static uint64_t threadID = Scheduler::GetThreadId(std::this_thread::get_id());

void testScheduler()
{
    static int times = 5;
    log_debug("testScheduler run thread id: {}", Scheduler::GetThreadId(std::this_thread::get_id()));
    sleep(1);

    if(--times > 0)
    {
        Scheduler::GetThis()->schedule(testScheduler);
    }
}

void testState()
{
    while(true)
    {
        log_debug("testState run thread id: {}", Scheduler::GetThreadId(std::this_thread::get_id()));
        // Fiber::YieldToReady();
        Fiber::YieldToHold();
    }
}

int main(void)
{
    // testThreadId();
    Scheduler sc(3, true, "TEST");
    sc.start();
    // sc.schedule(testScheduler);
    sc.schedule(testState);
    sc.stop();
    // sleep(2);
    return 0;
}